package edu.gznu.ics.fast.pulsar.controller.prestoacc.picController;

import edu.gznu.ics.fast.common.UIHelper.PagiNationHelper;
import edu.gznu.ics.fast.common.UIHelper.PicSqlQueryPagiNationHelper;
import edu.gznu.ics.fast.common.sqlCheckHelper.SqlCheckHelper;
import edu.gznu.ics.fast.pulsar.model.vo.web.PicQueryResultVO;
import edu.gznu.ics.fast.pulsar.model.vo.web.QueryCommandVO;
import edu.gznu.ics.fast.pulsar.service.prestoacc.picService.PicInfoQueryService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by fei-zyx on 2016/10/19.
 */
@Controller

public class PicSqlQueryController {
    @Autowired
    private PicInfoQueryService picInfoQueryService;

    @RequestMapping(method = RequestMethod.POST, value = "/picSqlQuery", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<PicQueryResultVO> picSqlQuery(@RequestBody List<QueryCommandVO> queryCommandVO,
                                              HttpServletResponse response) throws IOException {


        Subject subject = SecurityUtils.getSubject();
        String user = subject.getPrincipal().toString();
        queryCommandVO.get(0).setSql(SqlCheckHelper.sqlChecker(queryCommandVO.get(0).getSql()));//sql checker;
        final int pageSize = 20;

        List<PicQueryResultVO> picQueryResultVOList = picInfoQueryService.findPicInfoBySql(queryCommandVO.get(0), user);


        PicSqlQueryPagiNationHelper picSqlQueryPagiNationHelper = new PicSqlQueryPagiNationHelper();


        return picSqlQueryPagiNationHelper.produceMeassage(pageSize, queryCommandVO, picQueryResultVOList);


      /*  if(queryCommandVO.get(0).getQueryIndex()==0){}







        int size=picQueryResultVOList.size();



      //  String part = Double.toString(Math.ceil((double) size / (double) pageSize));
        int part = (int) (Math.ceil((double) size / (double) pageSize));
        int existNextPart;
        if(queryCommandVO.get(0).getQueryIndex()==part){
        existNextPart=0;}
        else {existNextPart=1;
        }


        PicQueryResultVO PagePartMessageVO = new PicQueryResultVO();
        PagePartMessageVO.setPart(part);
        PagePartMessageVO.setExistNextPart(existNextPart);
        PagePartMessageVO.setSize(size);


              //  int aa=0;
      //  System.out.print(queryCommandVO.get(0).getSql());
        List<PicQueryResultVO> returnpicQueryResultVOList ;
        int begin=queryCommandVO.get(0).getQueryIndex()*  pageSize ;
        int end=(queryCommandVO.get(0).getQueryIndex()+1) *  pageSize;
        if(end<size) {
           returnpicQueryResultVOList = picQueryResultVOList.subList(begin,end);
            returnpicQueryResultVOList.add(PagePartMessageVO);
            return  returnpicQueryResultVOList;
        }else
        {
            returnpicQueryResultVOList = picQueryResultVOList.subList(begin, size);
            returnpicQueryResultVOList.add(PagePartMessageVO);
            return  returnpicQueryResultVOList;
        }*/

    }

    public String getUser() {

        return SecurityUtils.getSubject().getPrincipal().toString();
    }

    @RequestMapping(method = RequestMethod.POST, value = "/picSqlReQuery", produces = "application/json;charset=UTF-8")
    @ResponseBody
    //@CacheEvict(value = "picQueryCache", key="queryCommandVO.sql+#this.getUser()")
    public List<PicQueryResultVO> picSqlreQuery(@RequestBody List<QueryCommandVO> queryCommandVO,
                                                HttpServletResponse response) throws IOException {
        return picSqlQuery(queryCommandVO, response);

    }
}

